package com.qf.controller;


import com.qf.dto.R;
import com.qf.pojo.Category;
import com.qf.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoryController {

    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping("/category")
    public R addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return R.setOK();
    }

    @GetMapping("/categories")
    public R getAllCategories(){
        List<Category> categoryList = categoryService.selectAllCategories();
        return R.setOK(categoryList);
    }

    @GetMapping("/category/{id}")
    public R getCategoryById(@PathVariable Long id) {
        Category category = categoryService.selectById(id);
        return R.setOK(category);
    }

    @GetMapping("/category/")
    public R getCategoryById(String cName) {
        List<Category> categoryList = categoryService.selectCategoriesByNameLike(cName);
        return R.setOK(categoryList);
    }

    @DeleteMapping("/categories")
    public R deleteCategory(@RequestBody List<Long> ids){
        int result = categoryService.deleteCategoriesByIds(ids);
        return R.setOK();
    }
}
